<extend name="Public/base"/>

<block name="body">
    <!-- 標題欄 -->
    <div class="main-title">
        <h2>數據備份</h2>
    </div>
    <!-- /標題欄 -->

    <div class="cf">
        <a id="export" class="btn" href="javascript:;" autocomplete="off">立即備份</a>
        <a id="optimize" class="btn" href="{:U('optimize')}">優化表</a>
        <a id="repair" class="btn" href="{:U('repair')}">修復表</a>
    </div>

    <!-- 應用列表 -->
    <div class="data-table table-striped">
        <form id="export-form" method="post" action="{:U('export')}">
            <table>
                <thead>
                    <tr>
                        <th width="48"><input class="check-all" checked="chedked" type="checkbox" value=""></th>
                        <th>表名</th>
                        <th width="120">數據量</th>
                        <th width="120">數據大小</th>
                        <th width="160">創建時間</th>
                        <th width="160">備份狀態</th>
                        <th width="120">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <volist name="list" id="table">
                        <tr>
                            <td class="num">
                                <input class="ids" checked="chedked" type="checkbox" name="tables[]" value="{$table.name}">
                            </td>
                            <td>{$table.name}</td>
                            <td>{$table.rows}</td>
                            <td>{$table.data_length|format_bytes}</td>
                            <td>{$table.create_time}</td>
                            <td class="info">未備份</td>
                            <td class="action">
                                <a class="ajax-get no-refresh" href="{:U('optimize?tables='.$table['name'])}">優化表</a>&nbsp;
                                <a class="ajax-get no-refresh" href="{:U('repair?tables='.$table['name'])}">修復表</a>
                            </td>
                        </tr>
                    </volist>
                </tbody>
            </table>
        </form>
    </div>
    <!-- /應用列表 -->
</block>

<block name="script">
    <script type="text/javascript">
    (function($){
        var $form = $("#export-form"), $export = $("#export"), tables
            $optimize = $("#optimize"), $repair = $("#repair");

        $optimize.add($repair).click(function(){
            $.post(this.href, $form.serialize(), function(data){
                if(data.status){
                    updateAlert(data.info,'alert-success');
                } else {
                    updateAlert(data.info,'alert-error');
                }
                setTimeout(function(){
	                $('#top-alert').find('button').click();
	                $(that).removeClass('disabled').prop('disabled',false);
	            },1500);
            }, "json");
            return false;
        });

        $export.click(function(){
            $export.parent().children().addClass("disabled");
            $export.html("正在發送備份請求...");
            $.post(
                $form.attr("action"),
                $form.serialize(),
                function(data){
                    if(data.status){
                        tables = data.tables;
                        $export.html(data.info + "開始備份，請不要關閉本頁面！");
                        backup(data.tab);
                        window.onbeforeunload = function(){ return "正在備份數據庫，請不要關閉！" }
                    } else {
                        updateAlert(data.info,'alert-error');
                        $export.parent().children().removeClass("disabled");
                        $export.html("立即備份");
                        setTimeout(function(){
        	                $('#top-alert').find('button').click();
        	                $(that).removeClass('disabled').prop('disabled',false);
        	            },1500);
                    }
                },
                "json"
            );
            return false;
        });

        function backup(tab, status){
            status && showmsg(tab.id, "開始備份...(0%)");
            $.get($form.attr("action"), tab, function(data){
                if(data.status){
                    showmsg(tab.id, data.info);

                    if(!$.isPlainObject(data.tab)){
                        $export.parent().children().removeClass("disabled");
                        $export.html("備份完成，點擊重新備份");
                        window.onbeforeunload = function(){ return null }
                        return;
                    }
                    backup(data.tab, tab.id != data.tab.id);
                } else {
                    updateAlert(data.info,'alert-error');
                    $export.parent().children().removeClass("disabled");
                    $export.html("立即備份");
                    setTimeout(function(){
    	                $('#top-alert').find('button').click();
    	                $(that).removeClass('disabled').prop('disabled',false);
    	            },1500);
                }
            }, "json");

        }

        function showmsg(id, msg){
            $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
        }
    })(jQuery);
    </script>
</block>